package com.hktf.uilib.widget;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.PowerManager;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.hktf.corelib.utils.LogMds;
import com.hktf.uilib.R;

import java.util.List;

/**
 * author  YongLiu
 * date  2019/2/22.
 * desc:
 */

public class FloatingActionWindow extends LinearLayout implements View.OnTouchListener {
    private WindowManager windowManager;
    private WindowManager.LayoutParams params;

    public FloatingActionWindow(Context context) {
        super(context);

        // 设置悬浮按钮的图标
//        setImageResource(R.drawable.icon24_freeze_normal);

        // 创建WindowManager和LayoutParams对象
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                PixelFormat.TRANSLUCENT
        );

        // 设置悬浮按钮的显示位置
        params.gravity = Gravity.TOP | Gravity.START;
//        params.x = 0;
        params.x = 0;
        params.y = 0;
        setBackgroundColor(R.color.black);

        // 设置悬浮按钮的触摸监听器
//        setOnTouchListener(this);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        LogMds.e("cdz","action66666="+ev.getAction()+"...x="+ev.getX()+"...y="+ev.getY());
        return super.dispatchTouchEvent(ev);
    }

    private int initialX;
    private int initialY;
    private float initialTouchX;
    private float initialTouchY;
    private boolean isClick;
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // 记录初始位置和触摸点坐标
                initialX = params.x;
                initialY = params.y;
                initialTouchX = event.getRawX();
                initialTouchY = event.getRawY();
                isClick=true;
                LogMds.e("cdz","isClick1...."+isClick);
                return true;
            case MotionEvent.ACTION_MOVE:
                // 计算偏移量
                int offsetX = (int) (event.getRawX() - initialTouchX);
                int offsetY = (int) (event.getRawY() - initialTouchY);

                // 更新悬浮按钮的位置
                params.x = initialX + offsetX;
                params.y = initialY + offsetY;
                windowManager.updateViewLayout(this, params);
                if (Math.abs(offsetX)>10||Math.abs(offsetY)>10) {
                    isClick=false;
                }
                LogMds.e("cdz","isClick2...."+isClick);
                return true;
            case MotionEvent.ACTION_UP:
                // 松开时处理事件
                // 这里可以实现按钮的点击事件
                // 点击事件可以执行特定的操作
                LogMds.e("cdz","isClick...."+isClick);
                if(isClick){
                    LogMds.e("cdz","点击了....");
                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                        // 获取ActivityManager实例
                        ActivityManager activityManager = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);

                        // 获取应用的任务栈
                        List<ActivityManager.AppTask> appTasks = null;
                        appTasks = activityManager.getAppTasks();
                        LogMds.e("cdz","点击了....appTasks="+appTasks.size());

                        // 判断应用的任务栈是否为空
                        if (appTasks != null && appTasks.size() > 0) {
                            // 将应用的主Activity移到前台
                            appTasks.get(0).moveToFront();
                        }
                    }

                }
                return true;
        }
        return false;
    }
    private PowerManager.WakeLock wakeLock;

    // 显示悬浮按钮
    public void show() {
        windowManager.addView(this, params);
        // 获取电源管理器
        PowerManager powerManager = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE);

        if (wakeLock == null) {
            // 创建唤醒锁
            wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                    "FloatingActionButton:WakeLock");
        }
        // 获取唤醒锁并保持屏幕唤醒状态
        wakeLock.acquire();
    }
    // 显示悬浮按钮
    public void setLayout(int x,int y) {
        params.x=x;
        params.y=y;
        windowManager.updateViewLayout(this, params);
    }

    // 隐藏悬浮按钮
    public void hide() {
        windowManager.removeView(this);
        // 释放唤醒锁
        if (wakeLock != null && wakeLock.isHeld()) {
            wakeLock.release();
        }
    }
}
